<template>
  <div>
    <!-- 新增和编辑数据字典的Dialog -->
    <el-dialog :title="option.title"  :visible.sync="isShow" :close-on-click-modal="false"
      @close="closeDialog('ruleForm')" @open="openDialog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" size="small">
        <el-row>
          <el-col :span="16">
            <el-form-item label="机构号" prop="branchName">
              <!-- <el-popover placement="bottom" width="200" trigger="click" v-model="popoverShow1">
                <el-tree :data="treedata" :highlight-current="true" :expand-on-click-node="false" default-expand-all :props="defaultProps" @node-click="handleNodeClick1">
                </el-tree>
                <el-input slot="reference" size="small" v-model="ruleForm.branchName" placeholder="请选择机构" :disabled="dialogTitle=='修改班次事件'"></el-input>
              </el-popover> -->
              <el-input v-model.trim="ruleForm.branchName" size="small" :disabled="option.title=='修改班次事件'"
                v-if="option.title=='修改班次事件'"></el-input>
              <select-tree  :branch="ruleForm.branchName" @getBranchName="getBranch1"
                v-if="option.title!='修改班次事件'"></select-tree>
            </el-form-item>
          </el-col>
          <el-col :span="16" v-if="ruleForm.CLASSESEVENTID">
            <el-form-item label="事件编号" prop="CLASSESEVENTID">
              <el-input v-model="ruleForm.CLASSESEVENTID" size="small" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="班次事件名称" prop="CLASSESEVENTNAME">
              <el-input v-model.trim="ruleForm.CLASSESEVENTNAME" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="活动类型" prop="ACTIVITYTYPE">
              <el-select v-model="ruleForm.ACTIVITYTYPE" size="small" placeholder="活动类型">
                <el-option v-for="item in actOptions" :key="item.ACTIVITYTYPE" :label="item.ACTIVITYTYPENAME"
                  :value="item.ACTIVITYTYPE"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="时长" prop="HOURS">
              <el-time-picker size="small" @focus="ChangeTime('HOURS')" v-model="ruleForm.HOURS" format="HH:mm"
                value-format="HH:mm" style="width: 100%" placeholder="请选择时长" @change="changeHour">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="开始时间类型" prop="SRTTIMETYPE">
              <el-radio-group v-model="ruleForm.SRTTIMETYPE" :disabled="ruleForm.HOURS==''">
                <el-col :span="13" style="margin-bottom:15px;">
                  <el-radio :label="'0'">绝对时间
                    <el-time-picker v-model="startTime0" @focus="ChangeTime('startTime0')" size="small"
                      style="width: 60%" format="HH:mm" value-format="HH:mm" :disabled="ruleForm.SRTTIMETYPE!='0'"
                      placeholder="开始时间">
                    </el-time-picker>
                    <el-time-picker v-model="endTime" size="small" @focus="ChangeTime('endTime')" style="width: 60%"
                      format="HH:mm" value-format="HH:mm" :disabled="ruleForm.SRTTIMETYPE!='0'" placeholder="结束时间">
                    </el-time-picker>
                  </el-radio>
                </el-col>
                <el-col :span="13" style="margin-bottom:15px;">
                  <el-radio :label="'1'">相对于班次开始时间后
                    <el-time-picker v-model="startTime1" @focus="ChangeTime('startTime1')" size="small"
                      style="width: 60%" format="HH:mm" value-format="HH:mm" :disabled="ruleForm.SRTTIMETYPE!='1'"
                      placeholder="请选择时间">
                    </el-time-picker>
                  </el-radio>
                </el-col>
                <el-col :span="13" style="margin-bottom:15px;">
                  <el-radio :label="'2'">相对于班次结束时间前
                    <el-time-picker v-model="startTime2" @focus="ChangeTime('startTime2')" size="small"
                      style="width: 60%" format="HH:mm" value-format="HH:mm" :disabled="ruleForm.SRTTIMETYPE!='2'"
                      placeholder="请选择时间">
                    </el-time-picker>
                  </el-radio>
                </el-col>
                <el-col :span="13" style="margin-bottom:15px;">
                  <el-radio :label="'3'">任意时间</el-radio>
                </el-col>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="13">
            <el-form-item label="最早开始时间" prop="EARLIESTSRTTIME">
              <el-time-select v-model="ruleForm.EARLIESTSRTTIME" size="small" style="width: 100%" :picker-options="{
                  start: '00:00',
                  step: '0:30',
                  end: '24:00'
                }" format="HH:mm" value-format="HH:mm" placeholder="最早开始时间" :disabled="ruleForm.SRTTIMETYPE=='0'">
              </el-time-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="13">
            <el-form-item label="最晚开始时间" prop="LATESTSRTTIME">
              <el-time-select v-model="ruleForm.LATESTSRTTIME" size="small" style="width: 100%" :picker-options="{
                  start: '00:00',
                  step: '0:30',
                  end: '24:00'
                }" format="HH:mm" value-format="HH:mm" placeholder="最晚开始时间" :disabled="ruleForm.SRTTIMETYPE=='0'">
              </el-time-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="最少次数" prop="MINTIMES">
              <el-input-number v-model="ruleForm.MINTIMES" size="small" :min="0" :max="9"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最多次数" prop="MAXTIMES">
              <el-input-number v-model="ruleForm.MAXTIMES" size="small" :min="0" :max="9"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否带薪">
              <el-switch v-model="ruleForm.ISPAID" active-value="1" inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="说明" prop="NOTES">
              <el-input v-model.trim="ruleForm.NOTES" type="textarea" :rows="2" size="small"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11" :offset="13">
            <el-form-item>
              <el-button type="primary" size="small" @click="submitForm('ruleForm')">保存</el-button>
              <el-button size="small" type="danger" @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import request from "@/common/utils/requestWFM";
  import selectTree from "@/components/selectTree5/";
  import ActivityQuery from '@/message/wfm/scheduleDataManage/activityManage/actQuery'
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import Update from '@/message/wfm/scheduleDataManage/classEventManage/classEventUpdate'
  import Add from '@/message/wfm/scheduleDataManage/classEventManage/classEventAdd'
  export default {
    components: {
      selectTree
    },
    props: {
      option: Object
    },
    watch: {
      // 'ruleForm.SRTTIMETYPE':function(val){
      //   console.log(val)
      //   if(val == '0'){
      //     this.ruleForm.EARLIESTSRTTIME = '0:00'
      //     this.ruleForm.LATESTSRTTIME = '0:00'
      //   }
      // }
      // endTime(val) {
      //   // console.log('end',val)
      //   if (val == '00:00') {
      //     let time = parseInt(this.ruleForm.HOURS.split(':')[0]) * 60 * 60 + parseInt((this.ruleForm.HOURS.split(':')[
      //       1])) * 60
      //     time = 24 * 60 * 60 - time
      //     let hour = parseInt(time / 60 / 60) < 10 ? '0' + parseInt(time / 60 / 60) : parseInt(time / 60 / 60)
      //     let min = (time - parseInt(time / 60 / 60) * 60 * 60) / 60
      //     hour = hour < 10 ? '0' + hour : hour
      //     min = min < 10 ? '0' + min : min
      //     this.startTime0 = hour + ':' + min
      //   }
      // },
      // startTime0(val) {
      //   // console.log('start', val)
      //   if (val == '00:00') {
      //     this.endTime = this.ruleForm.HOURS
      //     return
      //   }
      // }
    },
    computed: {
      isShow: {
        get() {
          return this.option.isShow;
        },
        set(value) {
          this.option.isShow = value;
        }
      }
    },
    data() {
      return {
        ChangeTime(data) {
          var time = '00:00';
          var that = this;
          if (data == 'HOURS') {
            if (this.ruleForm.HOURS == '') {
              this.ruleForm.HOURS = time;
            }
          } else if (data == 'startTime0') {
            if (this.startTime0 == '') {
              this.startTime0 = time;
            }
          } else if (data == 'endTime') {
            if (this.endTime == '') {
              this.endTime = time;
            }
          } else if (data == 'startTime1') {
            if (this.startTime1 == '') {
              this.startTime1 = time;
            }
          } else if (data == 'startTime2') {
            if (this.startTime2 == '') {
              this.startTime2 = time;
            }
          }
        },
        popoverShow1: false,
        startTime0: '',
        startTime1: '',
        startTime2: '',
        endTime: '',
        actOptions: [], //活动类型
        ruleForm: {
          branchName: '',
          branchNO: this.$store.getters.BranchNum,
          CLASSESEVENTID: '',
          CLASSESEVENTNAME: '',
          NOTES: '',
          ACTIVITYTYPE: '',
          HOURS: '',
          SRTTIMETYPE: '',
          SRTTIME: '',
          ENDTIME: '',
          ISPAID: '0',
          MINTIMES: '1',
          MAXTIMES: '1'
        },
        rules: {
          SRTTIMETYPE: [{
            required: true,
            message: '请选择开始时间类型',
            trigger: 'change'
          }],
          branchName: [{
            required: true,
            message: '请选择机构',
            trigger: 'change'
          }],
          CLASSESEVENTNAME: [{
              required: true,
              message: '请输入班次事件名称',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 25,
              message: '长度在 1 到 25 个字符',
              trigger: 'blur'
            }
          ],
          HOURS: [{
            required: true,
            message: '请选择时长',
            trigger: 'change'
          }],
          ISPAID: [{
            required: true,
            message: '请选择是否带薪',
            trigger: 'change'
          }],
          ACTIVITYTYPE: [{
            required: true,
            message: '请选择活动类型',
            trigger: 'change'
          }],
          NOTES: [{
            min: 1,
            max: 250,
            message: '长度在 1 到 250 个字符',
            trigger: 'blur'
          }],
        },
        input: '',
        select: '',
      }
    },
    methods: {
      changeHour() {
        if (this.ruleForm.SRTTIMETYPE == '0') {
          this.startTime0 = ''
          this.endTime = ''
        }
      },
       // changeStartTime(val) {
      //   if (val == null) {
      //     this.endTime = ''
      //     return
      //   }
      //   if (val == '00:00') {
      //     this.endTime = this.ruleForm.HOURS
      //     return
      //   }
      //   if (val != '') {
      //     let start = parseInt(val.split(':')[0]) * 60 * 60 + parseInt(val.split(':')[1]) * 60
      //     let time = parseInt(this.ruleForm.HOURS.split(':')[0]) * 60 * 60 + parseInt((this.ruleForm.HOURS.split(':')[
      //       1])) * 60
      //     // let hour = parseInt(val.split(':')[0]) + parseInt(this.ruleForm.HOURS.split(':')[0])
      //     // let min = parseInt(val.split(':')[1]) + parseInt(this.ruleForm.HOURS.split(':')[1])
      //     let end = time + start
      //     let hour = parseInt(end / 60 / 60)
      //     let min = (end - hour * 60 * 60) / 60
      //     hour = hour >= 24 ? (hour - 24) : hour
      //     hour = hour < 10 ? '0' + hour : hour
      //     min = min < 10 ? '0' + min : min
      //     this.endTime = hour + ':' + min
      //   }
      // },
      // changeEndTime(val) {
      //   // if(val == '00:00'){
      //   //     this.startTime0 = this.ruleForm.fa-hourglass-start
      //   //     return 
      //   //   }
      //   if (this.endTime == null) {
      //     this.startTime0 = ''
      //     return
      //   }
      //   if (this.ruleForm.HOURS) {
      //     // if(this.startTime0){
      //     let end = parseInt(this.endTime.split(':')[0]) * 60 * 60 + parseInt(this.endTime.split(':')[1]) * 60
      //     let start = parseInt(this.ruleForm.HOURS.split(':')[0]) * 60 * 60 + parseInt(this.ruleForm.HOURS.split(':')[
      //       1]) * 60
      //     let time
      //     if (end < start) {
      //       time = end + 24 * 60 * 60 - start
      //     } else {
      //       time = end - start
      //     }
      //     let hour = parseInt(time / 60 / 60) < 10 ? '0' + parseInt(time / 60 / 60) : parseInt(time / 60 / 60)
      //     let min = (time - parseInt(time / 60 / 60) * 60 * 60) / 60
      //     hour = hour < 10 ? '0' + hour : hour
      //     min = min < 10 ? '0' + min : min
      //     this.startTime0 = hour + ':' + min
      //     // }else{
      //     //   // this.startTime0 = this.endTime - this.ruleForm.HOURS
      //     // }
      //   }

      // },
      openDialog() {
        if (this.$refs['ruleForm'] != undefined) {
          this.$refs['ruleForm'].clearValidate();
          this.$refs['ruleForm'].resetFields();
        }
        if (this.option.title == '修改班次事件') {
          this.ruleForm = JSON.parse(JSON.stringify(this.option.row));
          if (this.ruleForm.SRTTIMETYPE == '0') {
            this.startTime0 = this.ruleForm.SRTTIME
            this.endTime = this.ruleForm.ENDTIME
          } else if (this.ruleForm.SRTTIMETYPE == '1') {
            this.startTime1 = this.ruleForm.SRTTIME
          } else if (this.ruleForm.SRTTIMETYPE == '2') {
            this.startTime2 = this.ruleForm.SRTTIME
          }
        } else {
          this.ruleForm = {
            branchName: '',
            branchNO: '',
            CLASSESEVENTID: '',
            CLASSESEVENTNAME: '',
            NOTES: '',
            ACTIVITYTYPE: '',
            HOURS: '',
            SRTTIMETYPE: '',
            SRTTIME: '',
            ENDTIME: '',
            ISPAID: '0',
            MINTIMES: '1',
            MAXTIMES: '1'
          }
        }
        this.actQuery()
      },
      closeDialog(formName) {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
        this.ruleForm.branchName = ''
        this.ruleForm.branchNO = ''
        this.ruleForm.CLASSESEVENTID = ''
        this.ruleForm.CLASSESEVENTNAME = ''
        this.ruleForm.NOTES = ''
        this.ruleForm.ACTIVITYTYPE = ''
        this.ruleForm.HOURS = ''
        this.ruleForm.SRTTIMETYPE = ''
        this.ruleForm.SRTTIME = ''
        this.ruleForm.ENDTIME = ''
        this.ruleForm.ISPAID = ''
        this.startTime0 = ''
        this.startTime1 = ''
        this.startTime2 = ''
        this.endTime = ''
        this.ruleForm.MINTIMES = '1'
        this.ruleForm.MAXTIMES = '1'
      },
      getBranch1(data) {
        // console.log(data)
        this.ruleForm.branchName = data.BRANCHNAME
        this.ruleForm.branchNO = data.BRANCHNO
        this.popoverShow1 = false
      },
      // 活动查询
      async actQuery() {
        let resBody = new ActivityQuery()
        resBody.data.CurrPage = 1;
        resBody.data.PageRowNum = 20;
        resBody.data.ACTIVITYCATEG = '02';
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.actOptions = response.BODY.RSPSTRUCT
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
       submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            if (this.ruleForm.MINTIMES > this.ruleForm.MAXTIMES) {
              this.$message({
                type: "error",
                message: '最多次数不能小于最少次数'
              })
              return false
            }
            if (this.option.title == '修改班次事件') {
              let resBody = new Update()
              resBody.data = JSON.parse(JSON.stringify(this.ruleForm))
              if (this.ruleForm.SRTTIMETYPE == '0') {
                resBody.data.SRTTIME = this.startTime0
                resBody.data.ENDTIME = this.endTime
              } else if (this.ruleForm.SRTTIMETYPE == '1') {
                resBody.data.SRTTIME = this.startTime1
                resBody.data.ENDTIME = ''
              } else if (this.ruleForm.SRTTIMETYPE == '2') {
                resBody.data.SRTTIME = this.startTime2
                resBody.data.ENDTIME = ''
              } else if (this.ruleForm.SRTTIMETYPE == '3') {
                resBody.data.SRTTIME = ''
                resBody.data.ENDTIME = ''
              }
              let response = await request(resBody);
              if (response.SYS_HEAD.ReturnCode == "000000") {
                this.option.isShow = false
                this.$emit("ListenClass")
              } else {
                this.$message({
                  type: "error",
                  message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
                    .ReturnMessage
                })
              }
            } else {
              let resBody = new Add()
              resBody.data = JSON.parse(JSON.stringify(this.ruleForm))
              resBody.data.BRANCHNO = this.ruleForm.branchNO
              if (this.ruleForm.SRTTIMETYPE == '0') {
                resBody.data.SRTTIME = this.startTime0
                resBody.data.ENDTIME = this.endTime
              } else if (this.ruleForm.SRTTIMETYPE == '1') {
                resBody.data.SRTTIME = this.startTime1
                resBody.data.ENDTIME = ''
              } else if (this.ruleForm.SRTTIMETYPE == '2') {
                resBody.data.SRTTIME = this.startTime2
                resBody.data.ENDTIME = ''
              } else if (this.ruleForm.SRTTIMETYPE == '3') {
                resBody.data.SRTTIME = ''
                resBody.data.ENDTIME = ''
              }
              let response = await request(resBody);
              if (response.SYS_HEAD.ReturnCode == "000000") {
                // this.editIsShow = false
                this.$refs[formName].resetFields();
                this.$refs[formName].clearValidate();
                this.ruleForm.SRTTIMETYPE = ''
                this.ruleForm.SRTTIME = ''
                this.ruleForm.ENDTIME = ''
                this.ruleForm.ISPAID = ''
                this.startTime0 = ''
                this.startTime1 = ''
                this.startTime2 = ''
                this.endTime = ''
                this.ruleForm.MINTIMES = '1'
                this.ruleForm.MAXTIMES = '1'
                this.option.isShow = false
                this.$emit("ListenClass")
              } else {
                this.$message({
                  type: "error",
                  message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
                    .ReturnMessage
                })
              }
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields()
      },
    },
  }

</script>
<style lang="scss" scoped>

</style>
